Movies.js ➔ Movies   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
1 4
import App from "app";
2
3
function Movies() {
4
    this.serviceName = "Movies";
5
    this.basepath = "//api.themoviedb.org/3/";
6
7
    this.Request = App.ServicesContainer.get("AJAX");
8
    this.EM = App.EventManager;
9
}
10
11
/**
12
 * @param {Object} complement
13
 * @return {Object}
14
 */
15 4
Movies.prototype.makePayload = function (complement = {}) {
16
    return Object.assign({
17
        api_key: App.config("TMDB_API_KEY"),
18
        language: App.config("LANGUAGE")
19
    }, complement);
20
}
21
22 4
Movies.prototype.search = function (term, page = 1) {
23
    return new Promise((resolve, reject) => {
24
        const payload = this.makePayload({
25
            query: term,
26
            page: page
27
        });
28
29
        this.Request.send('get', this.basepath + "search/multi", payload, (res) =>{
30
            resolve(res.body.results);
31
        }, (err) => {
32 4
            const errMessage = err.status && err.status == 401 ?
33
                "You're not authorized to access this resource. You should verify the TMDB_API_KEY config on '.env.js' file." :
34
                err.message;
35
36
            reject(errMessage);
37
        });
38
    });
39
}
40
41
/**
42
 * @param {String} type (person|tv|movie)
43
 * @param {Number} id
44
 * @return {Promise}
45
 */
46
Movies.prototype.get = function (type, id) {
47
    return new Promise((resolve, reject) => {
48
        this.Request.send(
49
            'get',
50
            `${this.basepath}${type}/${id}`,
51
            this.makePayload(),
52
            res => resolve(res.body),
53
            err => reject(err)
54
        );
55
    });
56
}
57
58
/**
59
 * @param {String} path
60
 * @param {String|Number} width
61
 * @reutrn {String}
62
 */
63 4
Movies.prototype.getPosterURL = (path, width = 300) => {
64
    return `http://image.tmdb.org/t/p/w${width}/${path}`;
65
}
66
67
export default Movies;
68